/**
 * Created by jun on 15/8/14.
 */
var async = require('async');
var OrderM=require('../models/order.js');
var _VIEW_PATH = '../app_module/diy/views/';

/*
 * 根据订单的商品列表，查询商品信息
 * 传入good对象pid，sid
 * 传出商品名称，规格名称，商品图片
 * */
var chuliOrderList = function(orderlist){
    var orderlists= new Array();
    for(var i= 0;i<orderlist.length;i++){
        var orderone=new Object();
        orderone.orderid=orderlist[i].get('orderid');
        orderone.oid=orderlist[i].get('oid');
        orderone.objeid=orderlist[i].id;
        orderone.finalAmount=orderlist[i].get('finalAmount');
        orderone.total=orderlist[i].get('total');
        orderone.points=orderlist[i].get('point');
        orderone.storeName=orderlist[i].get('storeName');
        orderone.toName=orderlist[i].get('toName');
        orderone.toPhone=orderlist[i].get('toPhone');
        orderone.status=chuliStatus(orderlist[i].get('status'));
        orderone.shopId=chuliShopId(orderlist[i].get('shopId'));
        orderone.payway=orderlist[i].get('payway');
        orderone.paytime=orderlist[i].get('paytime');

        orderone.show_flag=1;

        if(orderlist[i].get('status') == 1500){
            //微信支付，并且创建时间在30分钟内，不显示去付款按钮
            orderone.show_flag=0;
        }else{
            orderone.show_flag=1;
        }

        orderone.paytimestr=new Date(parseInt(orderlist[i].get('paytime'))).toLocaleString();
        //if(!orderlist[i].get('address')){
        //    continue;
        //}
        //orderone.address=orderlist[i].get('address').address;
        //orderone.name=orderlist[i].get('address').name;

        orderone.goods=new Array();

        var ordergoods=orderlist[i].get('goods');
        for(var j=0;j<ordergoods.length;j++){
            var good=new Object();
            good.pid=ordergoods[j].pid;

            if(!ordergoods[j].pname){
                good.pname='商品名称';
            }else{
                good.pname=ordergoods[j].pname;
            }
            good.sid=ordergoods[j].sid;

            if(!ordergoods[j].sname){
                good.sname='未获取';
            }else{
                good.sname=ordergoods[j].sname;
            }

            good.size=ordergoods[j].size;
            good.price=ordergoods[j].price;

            if(!ordergoods[j].img){
                good.img='#';
            }else{
                good.img=ordergoods[j].img;
            }
            orderone.goods[j]=good;
        }
        orderlists.push(orderone);
    }
    return orderlists;
};

var chuliStatus=function(status){
    var stu='';
    switch (status){
        case -1:
            stu='撤销自提订单';
            break;
        case -2:
            stu='平台撤销';
            break;
        case 1000:
            stu='创建订单失败';
            break;
        case 8000:
            stu='待自提';
            break;
        case 9000:
            stu='已自提';
            break;
        case 9999:
            stu='已完成';
            break;
        default :
            stu='处理中';
            break;
    }
    return stu;

};


var chuliShopId=function(shopId){
    var stu='';
    switch (shopId){
        case 1:
            stu='果然总部';
            break;
        case 3:
            stu='虹桥坊店';
            break;
        case 4:
            stu='梅岭店';
            break;
        case 5:
            stu='望月店';
            break;
        case 6:
            stu='时代店';
            break;
        case 7:
            stu='三盛店';
            break;
        default :
            stu='果然总部';
            break;
    }
    return stu;

};

module.exports={
    /*
     * 查看自提订单列表
     */
    getDiyOrders:function(req,res,next){
        var uid=req.AV.user.id;
        OrderM.getDiyOrderListByAll(uid).then(function(orderlist){
            var orderlists=chuliOrderList(orderlist);
            res.render(_VIEW_PATH + 'order/myDiyOrderByAll',{listtype:orderlists,title:'自提订单记录'});
        });
    },
}